package com.ea.demo.autogenapi.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ea.demo.autogenapi.base.vo.DefaultResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import com.ea.demo.autogenapi.user.vo.*;
import com.ea.demo.autogenapi.user.model.*;
import com.ea.demo.autogenapi.user.service.*;


/**
* <p>
* 前端用户 前端控制器
* </p>
*
* @author AutoGenerator Model Class
* @since 2020-03-27
*/
@Slf4j
@Api(tags = {"前端用户"})
@RestController
@RequestMapping("/user/front-user")
public class FrontUserController {

    @Autowired
    private IFrontUserService  frontUserService;



    @PostMapping("/add")
    @ApiOperation("新增操作")
    public DefaultResponseVo<FrontUserApiWrapper.FrontUserVo> add(@RequestBody FrontUserApiWrapper.FrontUserVo frontUserVo){
        Assert.notNull(frontUserVo,"参数非法.");
        FrontUser frontUser = new FrontUser();
        BeanUtils.copyProperties(frontUserVo,frontUser);
        frontUserService.save(frontUser);
        frontUserVo.setId(frontUser.getId());
        return DefaultResponseVo.success("success",frontUserVo);
    }

    @PostMapping("/update")
    @ApiOperation("修改操作")
    public DefaultResponseVo<FrontUserApiWrapper.FrontUserVo> update(@RequestBody FrontUserApiWrapper.FrontUserVo frontUserVo){
        Assert.notNull(frontUserVo,"参数非法.");
        FrontUser frontUser = new FrontUser();
        BeanUtils.copyProperties(frontUserVo,frontUser);
        frontUserService.updateById(frontUser);
        return DefaultResponseVo.success("success",frontUserVo);
    }

    @GetMapping("/get")
    @ApiOperation("获取单记录操作")
    public DefaultResponseVo<FrontUserApiWrapper.FrontUserVo> get(@RequestParam Integer id){
        Assert.notNull(id,"参数非法.");
        FrontUser frontUser = frontUserService.getById(id);
        if(frontUser==null)
            return DefaultResponseVo.success("success",null);
        FrontUserApiWrapper.FrontUserVo frontUserVo = new FrontUserApiWrapper.FrontUserVo();
        BeanUtils.copyProperties(frontUser,frontUserVo);
        return DefaultResponseVo.success("success",frontUserVo);
    }
    @PostMapping("/query")
    @ApiOperation("查询操作")
    public DefaultResponseVo<Page<FrontUserApiWrapper.FrontUserVo>> query(@RequestBody FrontUserApiWrapper.FrontUserSearchVo frontUserSearchVo){
        Assert.notNull(frontUserSearchVo,"参数非法.");
        Page<FrontUser> page = new Page<>(frontUserSearchVo.getPageNo(),frontUserSearchVo.getPageSize());
        QueryWrapper<FrontUser> wrapper = new QueryWrapper<>();
        //TODO:查询条件添加
        Page<FrontUser> listPage = frontUserService.page(page,wrapper);
        //TODO:结果对象转换
        return DefaultResponseVo.success("success",null);
    }


}
